package net.csdn.business.common.utils;

import net.csdn.business.common.constant.AuthConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @author: zhangyalei
 * @Date: 2022/06/15 14:00
 * @Description:
 */
public class UserInfoParser {

    /**
     * 获取token
     *
     * @return
     */
    public static String getToken() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            return request.getAttribute(AuthConstants.TOKEN_KEY) == null ? "" : (String) request.getAttribute(AuthConstants.TOKEN_KEY);
        }
        return null;
    }

    public static final String getUserId() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String userId = "";
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            userId = request.getAttribute(AuthConstants.USER_ID) == null ? "" : (String) request.getAttribute(AuthConstants.USER_ID);
        }
        return userId;
    }

    public static final String getUsername() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String username = "";
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            username = request.getAttribute(AuthConstants.USER_NAME) == null ? "" : (String) request.getAttribute(AuthConstants.USER_NAME);
        }
        return username;
    }

    public static final String getNickName() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String nickName = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            nickName = request.getAttribute(AuthConstants.NICK_NAME) == null ? "" : (String) request.getAttribute(AuthConstants.NICK_NAME);
        }
        return nickName;
    }

    /*public static final String getUserNameSpace() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String userNameSpace = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            userNameSpace = request.getAttribute(AuthConstants.USER_NAME_SPACE) == null ? "" : (String) request.getAttribute(AuthConstants.USER_NAME_SPACE);
        }
        return userNameSpace;
    }*/

    public static final String getAdminRoleCode() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String adminRoleCode = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            adminRoleCode = request.getAttribute(AuthConstants.ADMIN_ROLE_CODE) == null ? "" : (String) request.getAttribute(AuthConstants.ADMIN_ROLE_CODE);
        }
        return adminRoleCode;
    }

    /**
     * 当前用户是否为模拟用户
     *
     * @return
     */
    public static final Boolean isSimulate() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String isSimulate = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            isSimulate = Objects.toString(request.getAttribute(AuthConstants.ATOMGIT_SIMULATE_SYMBOL), "");
        }
        return StringUtils.isNotBlank(isSimulate) && "1".equals(isSimulate);
    }

    /**
     * 模拟用户ID
     *
     * @return
     */
    public static final String getSimulateUserId() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String result = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            result = Objects.toString(request.getAttribute(AuthConstants.ATOMGIT_ADMIN_USER_ID), "");
        }
        return result;
    }

    /**
     * 模拟用户名
     *
     * @return
     */
    public static final String getSimulateUsername() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String result = null;
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            result = Objects.toString(request.getAttribute(AuthConstants.ATOMGIT_ADMIN_USER_NAME), "");
        }
        return result;
    }

}
